
package dialogs;

import core.COINCOMOComponent;
import core.COINCOMOSubComponent;
import core.COINCOMOSubSystem;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Set;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JSplitPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.border.TitledBorder;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.xml.ws.Response;

import main.COINCOMO;
import main.GlobalMethods;
import main.Icons;
import panels.ComponentOverviewPanel;
import database.COINCOMOSubSystemManager;


/**
 *
 * @author Raed Shomali
 */
public class CotClassDialog extends JDialog implements ActionListener, ChangeListener
{


	char popupflag='n';
	// Function Points

	JLabel jLabel = new JLabel();
	JLabel jLabel2 = new JLabel();


	JCheckBox aggregateCode = new JCheckBox("G00 Aggregate Glue Code");
	JCheckBox genericComponent = new JCheckBox("G01 Generic Component");
	JCheckBox backOfficeRetail = new JCheckBox("G02 Back Office Retail");
	JCheckBox communication = new JCheckBox("G03 Communication protocols/packages");
	JCheckBox compilers = new JCheckBox("G04 Compilers");
	JCheckBox configuration = new JCheckBox("G05 Configuration mgmt/build tools");
	JCheckBox dataConversionPackages = new JCheckBox("G06 Data Conversion packages");
	JCheckBox databases = new JCheckBox("G07 Databases");
	JCheckBox deviceDrivers = new JCheckBox("G08 Device Drivers");
	JCheckBox diskArrays = new JCheckBox("G09 Disk Arrays");
	JCheckBox emulators = new JCheckBox("G10 Emulators");
	JCheckBox engineeringTools = new JCheckBox("G11 Engineering tools (req mgmt, design)");
	JCheckBox graphicalInformationSystem = new JCheckBox("G12 Graphic Information System");
	JCheckBox gui = new JCheckBox("G13 GUIs/GUI builders");
	JCheckBox middleware = new JCheckBox("G14 Middleware");
	JCheckBox networkManagers = new JCheckBox("G15 Network Mangers");
	JCheckBox operatingSystems = new JCheckBox("G16 Operating Systems");
	JCheckBox problemMgmt = new JCheckBox("G17 Problem Mgmt");
	JCheckBox reportGenerators = new JCheckBox("G18 Report Generators");
	JCheckBox softwareProcessTools = new JCheckBox("G19 Software Process Tools");
	JCheckBox telecommunicationInfrastructure = new JCheckBox("G20 Telecommunication & Infrastructure");
	JCheckBox telemetryAnalysis = new JCheckBox("G21 Telemetry Analysis");
	JCheckBox telemetryProcessing = new JCheckBox("G22 Telemetry Processing");
	JCheckBox wordProcessing = new JCheckBox("G23 Word Processing");


	// Tabs and Buttons
	JPanel jContentPane = new JPanel();
	JPanel jPanelNorth = new JPanel();
	JSplitPane jSplitPane = new JSplitPane();
	JPanel jLeftPanel = new JPanel();
	JPanel jRightPanel = new JPanel();

	JTabbedPane tabs = new JTabbedPane();
	JButton applyButton = new JButton( "Apply" );
	JButton closeButton = new JButton( "Close" );
	COINCOMOComponent component = null;

	int rowNumber = 0;

	public CotClassDialog( JFrame owner , COINCOMOComponent component , int rowNumber )
	{
		super( owner );

		this.setModal( true );
		this.setTitle( "Cot Class - " + component.getName() );
		this.rowNumber = rowNumber;
		this.component = component;
		GlobalMethods.updateStatusBar( "Done." );



		// checkbox

		aggregateCode.addActionListener(this);
		genericComponent.addActionListener(this);
		backOfficeRetail.addActionListener(this);
		communication.addActionListener(this);
		compilers.addActionListener(this);
		configuration.addActionListener(this);
		dataConversionPackages.addActionListener(this);
		databases.addActionListener(this);
		deviceDrivers.addActionListener(this);
		diskArrays.addActionListener(this);
		emulators.addActionListener(this);
		engineeringTools.addActionListener(this);
		graphicalInformationSystem.addActionListener(this);
		gui.addActionListener(this);
		middleware.addActionListener(this);
		networkManagers.addActionListener(this);
		operatingSystems.addActionListener(this);
		problemMgmt.addActionListener(this);
		reportGenerators.addActionListener(this);
		softwareProcessTools.addActionListener(this);
		telecommunicationInfrastructure.addActionListener(this);
		telemetryAnalysis.addActionListener(this);
		telemetryProcessing.addActionListener(this);
		wordProcessing.addActionListener(this);

		// Buttons
		applyButton.addActionListener( this );
		closeButton.addActionListener( this );

		applyButton.setFocusable( false );
		closeButton.setFocusable( false );

		applyButton.setIcon( Icons.SAVE_ICON );
		closeButton.setIcon( Icons.CLOSE_ICON );

		// Panel Tabs

		JPanel jContentPane = new JPanel( null );
		JPanel jPanelNorth = new JPanel( null );
		JSplitPane jSplitPane = new JSplitPane( );
		JPanel jLeftPanel = new JPanel( null );
		JPanel jRightPanel = new JPanel( null );




		jLeftPanel.setLayout(new BoxLayout(jLeftPanel, BoxLayout.Y_AXIS));
		jLeftPanel.add(aggregateCode , null);
		jLeftPanel.add(genericComponent , null);
		jLeftPanel.add(backOfficeRetail, null);
		jLeftPanel.add(communication, null);
		jLeftPanel.add(compilers, null);
		jLeftPanel.add(configuration, null);
		jLeftPanel.add(dataConversionPackages, null);
		jLeftPanel.add(databases, null);
		jLeftPanel.add(deviceDrivers, null);
		jLeftPanel.add(diskArrays, null);
		jLeftPanel.add(emulators, null);
		jLeftPanel.add(engineeringTools, null);


		jRightPanel.setLayout(new BoxLayout(jRightPanel, BoxLayout.Y_AXIS));
		jRightPanel.add(graphicalInformationSystem, null);
		jRightPanel.add(gui , null);
		jRightPanel.add(middleware, null);
		jRightPanel.add(networkManagers, null);
		jRightPanel.add(operatingSystems, null);
		jRightPanel.add(problemMgmt, null);
		jRightPanel.add(reportGenerators, null);
		jRightPanel.add(softwareProcessTools, null);
		jRightPanel.add(telecommunicationInfrastructure, null);
		jRightPanel.add(telemetryAnalysis, null);
		jRightPanel.add(telemetryProcessing, null);
		jRightPanel.add(wordProcessing, null);

		jLabel.setText("This Dialog box enables the user to select different COTS classes");
		jLabel2.setText(" If G00 is selected then the others are not possible to select");



		// GUI
		JPanel southPanel = new JPanel();

		jPanelNorth.setLayout( new BoxLayout(jPanelNorth, BoxLayout.Y_AXIS));
		jPanelNorth.setPreferredSize(new Dimension(100, 50));
		jPanelNorth.add(jLabel, null);
		jPanelNorth.add(jLabel2, null);

		southPanel.add( applyButton );
		southPanel.add( closeButton );


		this.setLayout( new BorderLayout() );
		this.add( jContentPane , BorderLayout.CENTER);

		this.add(jPanelNorth , BorderLayout.PAGE_START);


		jContentPane.setLayout(new BorderLayout());
		jContentPane.add(jSplitPane , BorderLayout.CENTER);


		jSplitPane.setResizeWeight(0.5);
		jSplitPane.setLeftComponent( jLeftPanel );
		jSplitPane.setRightComponent( jRightPanel );

		this.add( southPanel , BorderLayout.SOUTH );

		// Loading ...
		loadParameters();
		
		stateCheck();
		
		this.setDefaultCloseOperation( JDialog.DISPOSE_ON_CLOSE );
		this.setLocation( this.getOwner().getX() + 100 , this.getOwner().getY() + 100 );
		this.setResizable( true );
		this.setSize(640 , 480 );
		this.setVisible( true );
	}

	/**
	 *
	 */
	public void loadParameters()
	{
		// Set Back To Default ...
		applyButton.setEnabled( true );
		closeButton.setEnabled( true );

		HashMap<Integer, int[]> cocotsClasses = component.getLoadedCocotsRatings();

		for(Integer cotClass : cocotsClasses.keySet())
		{
			if(cotClass.intValue() == 0)
			{
				aggregateCode.setSelected( true );
				break;
			}
			else
			{
				if ( cotClass.intValue() == 1 )
					genericComponent.setSelected( true );
				if ( cotClass.intValue() == 2 )
					backOfficeRetail.setSelected( true );
				if ( cotClass.intValue() == 3 )
					communication.setSelected( true );
				if ( cotClass.intValue() == 4 )
					compilers.setSelected( true );
				if ( cotClass.intValue() == 5 )
					configuration.setSelected( true );
				if ( cotClass.intValue() == 6 )
					dataConversionPackages.setSelected( true );
				if ( cotClass.intValue() == 7 )
					databases.setSelected( true );
				if ( cotClass.intValue() == 8 )
					deviceDrivers.setSelected( true );
				if ( cotClass.intValue() == 9 )
					diskArrays.setSelected( true );
				if ( cotClass.intValue() == 10 )
					emulators.setSelected( true );
				if ( cotClass.intValue() == 11 )
					engineeringTools.setSelected( true );
				if ( cotClass.intValue() == 12 )
					graphicalInformationSystem.setSelected( true );
				if ( cotClass.intValue() == 13 )
					gui.setSelected( true );
				if ( cotClass.intValue() == 14 )
					middleware.setSelected( true );
				if ( cotClass.intValue() == 15 )
					networkManagers.setSelected( true );
				if ( cotClass.intValue() == 16 )
					operatingSystems.setSelected( true );
				if ( cotClass.intValue() == 17 )
					problemMgmt.setSelected( true );
				if ( cotClass.intValue() == 18 )
					reportGenerators.setSelected( true );
				if ( cotClass.intValue() == 19 )
					softwareProcessTools.setSelected( true );
				if ( cotClass.intValue() == 20 )
					telecommunicationInfrastructure.setSelected( true );
				if ( cotClass.intValue() == 21 )
					telemetryAnalysis.setSelected( true );
				if ( cotClass.intValue() == 22 )
					telemetryProcessing.setSelected( true );
				if ( cotClass.intValue() == 23 )
					wordProcessing.setSelected( true );

			}

		}
		GlobalMethods.updateStatusBar( "Parameters Loaded." );
	}

	public void actionPerformed( ActionEvent e )
	{
		stateCheck();
		
		if ( e.getSource() == applyButton )
		{
			if( !component.getLoadedCocotsRatings().isEmpty() )
			{
				if ( component.getAggregateLoaded() )
				{
					if(checkSelections(0) && !aggregateCode.isSelected())
					{
						int n = JOptionPane.showConfirmDialog(this, "Warning the aggregate data will be lost, do you still want to continue?", "Warning",JOptionPane.YES_NO_OPTION);

						if(n == 0)
						{
							//delete "00"
							component.deleteLoadedCocotsRatings( new Integer(0) );
							component.clearCotClass();
							checkSelections(1);
							//new CotGlueCodeEAFTabDialog( COINCOMO.application , subSystem , rowNumber );
						}
						else
						{
							loadParameters();
						}
							
					}
					else
					{
						checkSelections(1);
						component.setCocotsRatings(0, component.getLoadedCocotsRatings().get(0));
						//new CotGlueCodeEAFTabDialog( COINCOMO.application , subSystem , rowNumber );
					}
				}
				
				else if( aggregateCode.isSelected() && !component.getAggregateLoaded() )
				{
					int n = JOptionPane.showConfirmDialog(this, "Warning the non-aggregate data will be lost, do you still want to continue?", "Warning",JOptionPane.YES_NO_OPTION);

					if(n == 0)
					{
						//delete varied
						component.clearLoadedCocotsRatings();
						component.clearCotClass();
						component.setAdditionalCotClass("Aggregate Glue Code");
						component.setAdditionalCotClassID(new Integer(0));
						//new CotGlueCodeEAFTabDialog( COINCOMO.application , subSystem , rowNumber );
					}
					else
					{
						loadParameters();
					}
				}
				
				else
				{
					component.clearCotClass();
					checkSelections(1);
					Set<Integer> loadedCotsIDS = component.getLoadedCocotsRatings().keySet();
					ArrayList<Integer> cocotsClassesSelected = component.getCotClassID();
					ArrayList<Integer> duplicateIDs = new ArrayList<Integer>();
					ArrayList<Integer> deleteLoadedIDs = new ArrayList<Integer>();
					
					if(loadedCotsIDS.size() >= cocotsClassesSelected.size())
					{
						for(Integer load : loadedCotsIDS)
						{
							if(cocotsClassesSelected.contains(load))
								component.setCocotsRatings(load, component.getLoadedCocotsRatings().get(load));
						}
							
					}
					
					//new CotGlueCodeEAFTabDialog( COINCOMO.application , subSystem , rowNumber );
				}
				
				
			}
			else
			{
				if(aggregateCode.isSelected())
				{
					component.setAdditionalCotClass("Aggregate Glue Code");
					component.setAdditionalCotClassID(new Integer(0));
					//new CotGlueCodeEAFTabDialog( COINCOMO.application , subSystem , rowNumber );
				}
				else
				{
					checkSelections(1);

					//new CotGlueCodeEAFTabDialog( COINCOMO.application , subSystem , rowNumber );
				}
			}
			this.dispose();
		}
		if ( e.getSource() == closeButton )
		{
			// Free Resources ... Close Window
			component.clearCotClass();
			this.dispose();
		}

		

	}

	public boolean checkSelections(int flag)
	{
		
		boolean nonAggregatedSelected = false;
		if ( aggregateCode.isSelected() )
		{
			if(flag != 0)
			{
				component.setAdditionalCotClass("Aggregate Glue Code");
				component.setAdditionalCotClassID(new Integer(0));
			}
			nonAggregatedSelected = true;
		}
		
		if ( genericComponent.isSelected() )
		{
			if(flag != 0)
			{
				component.setAdditionalCotClass("Generic Component");
				component.setAdditionalCotClassID(new Integer(1));
			}
			nonAggregatedSelected = true;
		}
		if ( backOfficeRetail.isSelected() )
		{
			if(flag != 0)
			{
				component.setAdditionalCotClass("Back Office Retail");
				component.setAdditionalCotClassID(new Integer(2));
			}
			nonAggregatedSelected = true;
		}
		if ( communication.isSelected() )
		{
			if(flag != 0)
			{
				component.setAdditionalCotClass("Communication protocols/packages");
				component.setAdditionalCotClassID(new Integer(3));
			}
			nonAggregatedSelected = true;
		}
		if ( compilers.isSelected() )
		{
			if(flag != 0)
			{
				component.setAdditionalCotClass("Compilers");
				component.setAdditionalCotClassID(new Integer(4));
			}
			nonAggregatedSelected = true;
		}
		if ( configuration.isSelected() )
		{
			if(flag != 0)
			{
				component.setAdditionalCotClass("Configuration mgmt/build tools");
				component.setAdditionalCotClassID(new Integer(5));
			}
			nonAggregatedSelected = true;

		}
		if ( dataConversionPackages.isSelected() )
		{
			if(flag != 0)
			{
				component.setAdditionalCotClass("Data Conversion Packages");
				component.setAdditionalCotClassID(new Integer(6));
			}
			nonAggregatedSelected = true;

		}
		if ( databases.isSelected() )
		{
			if(flag != 0)
			{
				component.setAdditionalCotClass("Databases");
				component.setAdditionalCotClassID(new Integer(7));
			}
			nonAggregatedSelected = true;

		}
		if ( deviceDrivers.isSelected() )
		{
			if(flag != 0)
			{
				component.setAdditionalCotClass("Device Drivers");
				component.setAdditionalCotClassID(new Integer(8));
			}
			nonAggregatedSelected = true;
		}
		if ( diskArrays.isSelected() )
		{
			if(flag != 0)
			{
				component.setAdditionalCotClass("Disk Arrays");
				component.setAdditionalCotClassID(new Integer(9));
			}
			nonAggregatedSelected = true;
		}
		if ( emulators.isSelected() )
		{
			if(flag != 0)
			{
				component.setAdditionalCotClass("Emulators");
				component.setAdditionalCotClassID(new Integer(10));
			}
			nonAggregatedSelected = true;
		}
		if ( engineeringTools.isSelected() )
		{
			if(flag != 0)
			{
				component.setAdditionalCotClass("Engineering tools (req mgmt, design)");
				component.setAdditionalCotClassID(new Integer(11));
			}
			nonAggregatedSelected = true;
		}
		if ( graphicalInformationSystem.isSelected() )
		{
			if(flag != 0)
			{
				component.setAdditionalCotClass("Graphical Information System");
				component.setAdditionalCotClassID(new Integer(12));
			}
			nonAggregatedSelected = true;
		}
		if ( gui.isSelected() )
		{
			if(flag != 0)
			{
				component.setAdditionalCotClass("GUIs/GUI builders");
				component.setAdditionalCotClassID(new Integer(13));
			}
			nonAggregatedSelected = true;
		}
		if ( middleware.isSelected() )
		{
			if(flag != 0)
			{
				component.setAdditionalCotClass("Middleware");
				component.setAdditionalCotClassID(new Integer(14));
			}
			nonAggregatedSelected = true;
		}
		if ( networkManagers.isSelected() )
		{
			if(flag != 0)
			{
				component.setAdditionalCotClass("Network Managers");
				component.setAdditionalCotClassID(new Integer(15));
			}
			nonAggregatedSelected = true;
		}
		if ( operatingSystems.isSelected() )
		{
			if(flag != 0)
			{
				component.setAdditionalCotClass("Operating Systems");
				component.setAdditionalCotClassID(new Integer(16));
			}
			nonAggregatedSelected = true;
		}
		if ( problemMgmt.isSelected() )
		{
			if(flag != 0)
			{
				component.setAdditionalCotClass("Problem mgmt");
				component.setAdditionalCotClassID(new Integer(17));
			}
			nonAggregatedSelected = true;
		}
		if ( reportGenerators.isSelected() )
		{
			if(flag != 0)
			{
				component.setAdditionalCotClass("Report Generators");
				component.setAdditionalCotClassID(new Integer(18));
			}
			nonAggregatedSelected = true;
		}
		if ( softwareProcessTools.isSelected() )
		{
			if(flag != 0)
			{
				component.setAdditionalCotClass("Software Process Tools");
				component.setAdditionalCotClassID(new Integer(19));
			}
			nonAggregatedSelected = true;
		}
		if ( telecommunicationInfrastructure.isSelected() )
		{
			if(flag != 0)
			{
				component.setAdditionalCotClass("Telecommunication & Infrastructure");
				component.setAdditionalCotClassID(new Integer(20));
			}
			nonAggregatedSelected = true;
		}
		if ( telemetryAnalysis.isSelected() )
		{
			if(flag != 0)
			{
				component.setAdditionalCotClass("Telemetry Analysis");
				component.setAdditionalCotClassID(new Integer(21));
			}
			nonAggregatedSelected = true;
		}
		if ( telemetryProcessing.isSelected() )
		{
			if(flag != 0)
			{
				component.setAdditionalCotClass("Telemetry Processing");
				component.setAdditionalCotClassID(new Integer(22));
			}
			nonAggregatedSelected = true;
		}
		if ( wordProcessing.isSelected() )
		{
			if(flag != 0)
			{
				component.setAdditionalCotClass("Word Processing");
				component.setAdditionalCotClassID(new Integer(23));
			}
			nonAggregatedSelected = true;
		}
		return nonAggregatedSelected;
	}

	public void stateCheck()
	{
		if ( aggregateCode.isSelected() || component.getAggregateLoaded() )
		{
			genericComponent.setEnabled( false );
			backOfficeRetail.setEnabled( false );
			communication.setEnabled( false );
			compilers.setEnabled( false );
			configuration.setEnabled( false );
			dataConversionPackages.setEnabled( false );
			databases.setEnabled( false );
			deviceDrivers.setEnabled( false );
			diskArrays.setEnabled( false );
			emulators.setEnabled( false );
			engineeringTools.setEnabled( false );
			graphicalInformationSystem.setEnabled( false );
			gui.setEnabled( false );
			middleware.setEnabled( false );
			networkManagers.setEnabled( false );
			operatingSystems.setEnabled( false );
			problemMgmt.setEnabled( false );
			reportGenerators.setEnabled( false );
			softwareProcessTools.setEnabled( false );
			telecommunicationInfrastructure.setEnabled( false );
			telemetryAnalysis.setEnabled( false );
			telemetryProcessing.setEnabled( false );
			wordProcessing.setEnabled( false );

		}
		if ( !aggregateCode.isSelected())
		{
			genericComponent.setEnabled( true );
			backOfficeRetail.setEnabled( true );
			communication.setEnabled( true );
			compilers.setEnabled( true );
			configuration.setEnabled( true );
			dataConversionPackages.setEnabled( true );
			databases.setEnabled( true );
			deviceDrivers.setEnabled( true );
			diskArrays.setEnabled( true );
			emulators.setEnabled( true );
			engineeringTools.setEnabled( true );
			graphicalInformationSystem.setEnabled( true );
			gui.setEnabled( true );
			middleware.setEnabled( true );
			networkManagers.setEnabled( true );
			operatingSystems.setEnabled( true );
			problemMgmt.setEnabled( true );
			reportGenerators.setEnabled( true );
			softwareProcessTools.setEnabled( true );
			telecommunicationInfrastructure.setEnabled( true );
			telemetryAnalysis.setEnabled( true );
			telemetryProcessing.setEnabled( true );
			wordProcessing.setEnabled( true );
		}
	}
	
	public void stateChanged( ChangeEvent e )
	{
		JTabbedPane pane = (JTabbedPane)e.getSource();

		//component.setSizingType( pane.getSelectedIndex() );
		
		if ( !aggregateCode.isSelected())
		{
			genericComponent.setEnabled( true );
			backOfficeRetail.setEnabled( true );
			communication.setEnabled( true );
			compilers.setEnabled( true );
			configuration.setEnabled( true );
			dataConversionPackages.setEnabled( true );
			databases.setEnabled( true );
			deviceDrivers.setEnabled( true );
			diskArrays.setEnabled( true );
			emulators.setEnabled( true );
			engineeringTools.setEnabled( true );
			graphicalInformationSystem.setEnabled( true );
			gui.setEnabled( true );
			middleware.setEnabled( true );
			networkManagers.setEnabled( true );
			operatingSystems.setEnabled( true );
			problemMgmt.setEnabled( true );
			reportGenerators.setEnabled( true );
			softwareProcessTools.setEnabled( true );
			telecommunicationInfrastructure.setEnabled( true );
			telemetryAnalysis.setEnabled( true );
			telemetryProcessing.setEnabled( true );
			wordProcessing.setEnabled( true );
		}
	}
}
